package com.jinzihan.service.impl;


import com.jinzihan.dao.ClassroomDao;
import com.jinzihan.dao.impl.ClassroomDaoImpl;
import com.jinzihan.entity.Classroom;
import com.jinzihan.entity.Page;
import com.jinzihan.service.ClassroomService;

import java.util.List;

public class ClassroomServiceImpl implements ClassroomService {
    ClassroomDao classroomDao = new ClassroomDaoImpl();
    @Override
    public void addClassroom(Classroom classroom) {
        classroomDao.saveClassroom(classroom);
    }

    @Override
    public void deleteClassroom(Integer id) {
        classroomDao.deleteClassroomById(id);
    }

    @Override
    public void updateClassroom(Classroom classroom) {
        classroomDao.updateClassroom(classroom);
    }

    @Override
    public Classroom queryClassroomById(int id) {
        return classroomDao.queryClassroomById(id);
    }

    @Override
    public List<Classroom> queryClassrooms() {
        return classroomDao.queryAllClassrooms();
    }

    @Override
    public Page<Classroom> page(int pageNo, int pageSize) {
        Page<Classroom> page = new Page<>();

        page.setPageSize(pageSize);

        Integer pageTotalCount = classroomDao.queryForClassroomCount();
        page.setPageTotalCount(pageTotalCount);

        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0 ){
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);

        page.setPageNo(pageNo);

        int begin = (page.getPageNo() - 1) * pageSize;
        List<Classroom> items = classroomDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
